package com.cloudm.technician.ui.modules.personal.change_nick_name

import com.cloudm.technician.data.db.DbHelper
import com.cloudm.technician.data.network.PersonalApi
import com.cloudm.technician.data.preferences.PreferencesHelper
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/26
 * 描述: 协议接口
 */
class ChangeNickNamePresenter(private val view: ChangeNickNameContract.View,
                              private val personalApi: PersonalApi,
                              private val preferencesHelper: PreferencesHelper,
                              private val dbHelper: DbHelper) : RxPresenter(),
        ChangeNickNameContract.Presenter {

    override fun changeNickName(name: String?) {
        if (name == null || name.isNullOrBlank()) {
            view.showNickNameEmpMsg()
            return
        }
        view.showChangeNickNameDialog()
        request(personalApi.modifyNickname(name), view, onSuccess = { _, _ ->
            view.hideChangeNickNameDialog()
            view.nickNameChangeSuccess(name)
            dbHelper.updateUserNickName(dbHelper.queryUserInfoByToken(preferencesHelper.token),
                    name)
        }, onFailure = { errorMsg ->
            view.hideChangeNickNameDialog()
            view.nickNameChangeFailure(errorMsg)
        })
    }

}